package com.fenghongzhang.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    private SQLiteDatabase readableDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //会
        MySqlHelp2 mySqlHelp2 = new MySqlHelp2(this, "person.db", null, 3);
         readableDatabase = mySqlHelp2.getReadableDatabase();

        //插入数据 ? 占位符
//        String sql = "insert into person values(null,?)";
//        readableDatabase.execSQL(sql,new Object[]{"猴子"});


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        readableDatabase.close();
    }

    public void btn(View view) {

//        readableDatabase.beginTransaction();
        //扣钱
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","哈哈");
        readableDatabase.insert("person",null,contentValues);
        int a = 10 / 0;
        //加100
        ContentValues contentValues1 = new ContentValues();
        contentValues1.put("name","哈哈");
        readableDatabase.insert("person",null,contentValues1);
//        readableDatabase.setTransactionSuccessful();//commit();

//        readableDatabase.endTransaction();
    }

    public void select(View view) {

//        String sql = "select name from person where name = 'xiaoming' and id = 1";
        readableDatabase.query("person", new String[]{"name"},"name = ? and id = ?",new String[]{"xiaoming","1"},null,null,null);
        Cursor cursor = readableDatabase.query("person", new String[]{"name"}, null, null, null, null, null);
        if(cursor != null){
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        }
    }
}
